package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.LikeBehaviorService;
import com.heima.behavior.thread.AppThreadLocalUtil;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LikeBehaviorServiceImpl implements LikeBehaviorService {

    @Autowired
    private CacheService cacheService;

    //点赞
    @Override
    public ResponseResult likeBehavior(LikesBehaviorDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        }

        Long articleId = dto.getArticleId();
        ApUser user = AppThreadLocalUtil.getUser();
        Integer userId = user.getId();
        if (userId == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"米有id");
        }

        Short operation = dto.getOperation();
        if (operation  == 0){
            Object o = cacheService.hGet(articleId.toString(), userId.toString());
            if (o != null){
                long l = System.currentTimeMillis();
                cacheService.hPut(articleId.toString(),userId.toString(),Long.toString(System.currentTimeMillis()));
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"不可重复点赞");
            }else {
                cacheService.hDelete(articleId.toString(),userId.toString());

            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    //阅读
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数为空");
        }

        Long articleId = dto.getArticleId();
        ApUser user = AppThreadLocalUtil.getUser();
        Integer userId = user.getId();
        if (userId == null || userId == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"游客登录");

        }
        Object readSum = cacheService.hGet(BehaviorConstants.READ_BEHAVIOR + articleId, userId.toString());

        //阅读次数
        Short count = dto.getCount();
        if (count == 0 && count >0){
            count = count++;
            dto.setCount(count);
        }
        cacheService.hPut(BehaviorConstants.READ_BEHAVIOR + articleId, userId.toString(), JSON.toJSONString(dto));



        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //喜欢不喜欢
    @Override
    public ResponseResult unLikeBehavior(UnLikesBehaviorDto dto) {

        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数为空");

        }
        Long articleId = dto.getArticleId();
        ApUser user = AppThreadLocalUtil.getUser();
        Integer userId = user.getId();

        //0不喜欢，1喜欢
        if (dto.getType() == 0) {
            Object o = cacheService.hGet(articleId.toString(), userId.toString());
            Long currentTimeMillis = System.currentTimeMillis();
            cacheService.hPut(BehaviorConstants.UN_LIKE_BEHAVIOR+articleId,userId.toString(),currentTimeMillis.toString());
        }
        else {
            cacheService.hDelete(articleId.toString(),user.toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
